﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace XMM.Controls.Library
{
    /// <summary>
    /// Interaktionslogik für Holster_CoverArt.xaml
    /// </summary>
    public partial class Holster_CoverArt : UserControl
    {
        /// <summary>
        /// Path to currently selected Cover-Image.
        /// </summary>
        public string CoverImagePath { get; set; }

        /// <summary>
        /// Current Cover-Status (has or has no).
        /// </summary>
        public bool HasCover { get; set; }

        /// <summary>
        /// Initialize a new Cover-Art-Holster-Control.
        /// </summary>
        public Holster_CoverArt()
        {
            InitializeComponent();

            HasCover = false;
        }

        /// <summary>
        /// Show Cover-Art-Editor-Dialoge.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEditCover_Click(object sender, RoutedEventArgs e)
        {
            Dialoges.Library.CoverArtEditor dlgEditCoverArt = new XMM.Dialoges.Library.CoverArtEditor(this, CoverImagePath);
            dlgEditCoverArt.ShowDialog();
        }

        /// <summary>
        /// Set Cover-Art-Status-Image.
        /// </summary>
        /// <param name="bIsEdited">(bool) [true] if Cover-Art was selected or [false] to show default icon. </param>
        public void setStatusImage(bool bIsEdited)
        {
            if (bIsEdited)
            {
                // Check if Image-File exists.
                if(System.IO.File.Exists(CoverImagePath))
                {
                    try
                    {
                        // If image-file exists, try to load it as thumbnail into btnEditCover.
                        imgCoverStatus.Source = new BitmapImage(new Uri(CoverImagePath, UriKind.Absolute));
                    }
                    catch (Exception ex)
                    {
                        releaseCurrentImage();
                    }
                }
            }
            else
            {
                releaseCurrentImage();
            }
        }

        /// <summary>
        /// Reset current Thumbnail to default icon.
        /// </summary>
        public void releaseCurrentImage()
        {
            imgCoverStatus.Source = new BitmapImage(new Uri(@"../../images/bin/new.png", UriKind.Relative));
        }

        /// <summary>
        /// Set new Cover-Art-Image.
        /// </summary>
        /// <param name="sCoverImagePath">(string) Path to new image-file.</param>
        public void setCoverImage(string sCoverImagePath)
        {
            // Check if image-file exists.
            if (System.IO.File.Exists(sCoverImagePath))
            {
                try
                {
                    // If image-file exits, try to load it as thumbnail into btnEditCover.
                    imgCoverStatus.Source = new BitmapImage(new Uri(sCoverImagePath, UriKind.Absolute));
                    CoverImagePath = sCoverImagePath;
                    HasCover = true;
                }
                catch (Exception ex)
                {
                    releaseCurrentImage();
                    CoverImagePath = String.Empty;
                    HasCover = false;
                }
            }
            else
            {
                releaseCurrentImage();
                CoverImagePath = String.Empty;
                HasCover = false;
            }
        }
    }
}
